<?php

/**
 * @author ntourne
 * Manejo del objeto TopicComment
 */

class Comment extends Entity {

	var $commentVO;
	
	function __construct($commentVO = null) {
		parent::__construct();
		$this->commentVO = $commentVO;
	}

	
	function fillVO() {
		$this->commentVO->user_id = $_SESSION['userid'];
		$this->commentVO->user_username = $_SESSION['username'];
	}
	
	
	function getVO() {
		return $this->commentVO;
	}
	
	
	function setVO($commentVO) {
		$this->commentVO = $commentVO;
	}
	
		
	/**
	 * Return list of comments by Topic id
	 * @return List<CommentVO>
	 */
	static function getCommentsByTopicId($commentVO) {

		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_CommentsByTopicId");
		$dbUtil->setParameter($commentVO->topic_id, ParameterDB::INT_TYPE);
		$sent = $dbUtil->generateSP();
		
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$comments = array();
		while ($row = mysql_fetch_array($sql)) {
			$comments[] = Comment::populateCommentVO($row);
		}
				
		$db->closeConnection();
		
		return $comments;
	}	
	
	
	
	/**
	 * Return list of comments by Username
	 * @return List<CommentVO>
	 */
	static function getCommentsByUsername($commentVO) {

		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_CommentsByUsername");
		$dbUtil->setParameter($commentVO->user_username, ParameterDB::VARCHAR_TYPE);
		$sent = $dbUtil->generateSP();
		
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$comments = array();
		while ($row = mysql_fetch_array($sql)) {
			$comments[] = Comment::populateCommentVO($row);
		}
				
		$db->closeConnection();
		
		return $comments;
	}	
	
	
	static function populateCommentVO($row) {
		$commentVO = new CommentVO();
		$commentVO->id = $row['id'];
		$commentVO->msg = $row['msg'];
		$commentVO->creationdate = $row['creationdate'];
		$commentVO->topic_id = $row['topic_id'];
		$commentVO->topic_name = $row['topic_name'];
		$commentVO->user_id = $row['user_id'];
		$commentVO->user_username = $row['user_username'];
		$commentVO->status = $row['status'];
		return $commentVO;
	}
	
	
	/**
	 * Return a Comments by id
	 * @return CommentVO
	 */
	static function getCommentsById($commentVO) {

		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_CommentsById");
		$dbUtil->setParameter($commentVO->id, ParameterDB::INT_TYPE);
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$row = mysql_fetch_array($sql);
		$db->closeConnection();
		
		if ($row == null)
			return false;
		
		$comment = Comments::populateCommentsVO($row);
		
		return $comment;
		
	}		
	
	
}
	

/**
 * @author ntourne
 * Manejo del objeto TopicCommentsVO
 */
class CommentVO {
	
	var $id;
	var $msg;
	var $creationdate;
	var $topic_id;
	var $topic_name;
	var $user_id;
	var $user_username;
	var $status;
	
	function __construct($o = null) {
		if ($o != null) {
			$this->id = $o['id'];
			$this->msg = $o['msg'];
			$this->creationdate = $o['creationdate'];
			$this->topic_id = $o['topic_id'];
			$this->topic_name = $o['topic_name'];
			$this->user_id = $o['user_id'];
			$this->user_username = $o['user_username'];
			$this->status = $o['status'];
		}
	}
		
	// apply linkify to msg
	function linkifyMsg() {
		$this->msg = Utilities::linkify($this->msg);
	}
	
	
}